package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleCollectionMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApCollectionService;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ArticleCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionServiceImpl extends ServiceImpl<ArticleCollectionMapper, ArticleCollection> implements ApCollectionService {

    @Autowired
    private ArticleCollectionMapper articleCollectionMapper;
    @Autowired
    private ApArticleService apArticleService;
    @Override
    public ResponseResult collection(CollectionBehaviorDto dto) {
        //条件判断
        if(dto == null || dto.getUserId() == null||dto.getArticleId()==null||dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断是否登录
        ApUser user = AppThreadLocalUtil.getUser();
        if(user == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //查询
        LambdaQueryWrapper<ArticleCollection> collectionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collectionLambdaQueryWrapper.eq(ArticleCollection::getUserId,dto.getUserId());
        collectionLambdaQueryWrapper.eq(ArticleCollection::getArticleId,dto.getArticleId());
        ArticleCollection one = getOne(collectionLambdaQueryWrapper);
        if(one==null){
            if(dto.getOperation()==1){

                ApArticle byId = apArticleService.getById(dto.getArticleId());
                Integer collection1 = byId.getCollection();
                collection1++;
                byId.setCollection(collection1);
                boolean b = apArticleService.updateById(byId);

                ArticleCollection collection = new ArticleCollection();
                collection.setUserId(dto.getUserId());
                collection.setArticleId(dto.getArticleId());
                collection.setCollectionTime(new Date());
                int insert = articleCollectionMapper.insert(collection);
                if(insert==1&&b){
                    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                }
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"未收藏");
            }
        }else{
            if(dto.getOperation()==0){
                ApArticle byId = apArticleService.getById(dto.getArticleId());
                Integer collection1 = byId.getCollection();
                collection1--;
                byId.setCollection(collection1);
                boolean b = apArticleService.updateById(byId);



                int delete = articleCollectionMapper.delete(collectionLambdaQueryWrapper);
                if(delete==1&&b){
                    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
                }
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"已经收藏过了");
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"收藏失败");
    }
}